package com.dycong.common.leetcode.yi;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: yilin
 * Date: 2019-08-10
 * Time: 14:50
 */
public class DeadLockDemo {

    public void method1() {
        synchronized (String.class) {
            System.out.println("Aquired lock on String.class object");

            synchronized (Integer.class) {
                System.out.println("Aquired lock on Integer.class object");
            }
        }
    }

    public void method2() {
        synchronized (Integer.class) {
            System.out.println("Aquired lock on Integer.class object");

            synchronized (String.class) {
                System.out.println("Aquired lock on String.class object");
            }
        }
    }

    //这样多个线上调用method1或者是method2就不会出现死锁了

    public void method1_F() {
        synchronized (String.class) {
            System.out.println("Aquired lock on String.class object");

            synchronized (Integer.class) {
                System.out.println("Aquired lock on Integer.class object");
            }
        }
    }

    public void method2_F() {
        synchronized (String.class) {
            System.out.println("Aquired lock on Integer.class object");

            synchronized (Integer.class) {
                System.out.println("Aquired lock on String.class object");
            }
        }
    }

}
